using System.Collections;
using UnityEngine;


[System.Serializable]
public class BossParameter
{
    public float BossCurrentHealth; // 当前血量
    public float BossMaxHealth ; // 最大血量
    public float BossDistortionLevel; // 失真度
    
    public float BossAttackCooldown; // 攻击冷却时间
    public float MeleeAttackValue;//扫击伤害
    
    public float BossDamageMultiplier;//敌人易伤系数
    public float BossAttackMultiplier;//敌人攻击系数
    
    public float BossBulletSpeed;//弹速
    public float BossBulletDamage;// 子弹伤害
    
}
public class BossController : MonoBehaviour
{
    private enum BossState
    {
        Idle,
        MeleeAttack,
        GunTransition,
        GunAttackLeft,
        GunAttackRight
    }

    public GameObject NextRoomObject;
    public AudioClip ShotSound;
    private AudioSource audioSource;
    public BossParameter bossParameter;
    public Animator animator;
    private BossState currentState;
    private float stateTimer;//等待时间
    public GameObject BossBulletPrefab;//Boss子弹
    public Transform[] FirePoint;

    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        animator = GetComponent<Animator>();
        bossParameter.BossCurrentHealth = bossParameter.BossMaxHealth;
        currentState = BossState.Idle;
        stateTimer = 0; 
    }

    void Update()
    {
        stateTimer -= Time.deltaTime;

        if (stateTimer <= 0)
        {
            SwitchState();
        }

        switch (currentState)
        {
            case BossState.MeleeAttack:
                PerformMeleeAttack();
                break;
            case BossState.GunAttackLeft:
            case BossState.GunAttackRight:
                PerformGunAttack();
                break;
        }
    }

    private void SwitchState()
    {
        int nextState = Random.Range(1, 3);

        switch (currentState)
        {
            case BossState.Idle:
                if (nextState == 1)
                {
                    currentState = BossState.MeleeAttack;
                    animator.Play("MeleeAttack");
                    stateTimer = bossParameter.BossAttackCooldown; // 挥砍动画的持续时间
                }
                else if (nextState == 2)
                {
                    currentState = BossState.GunTransition;
                    animator.Play("IdleToGun");
                    stateTimer = 1f; // 从Idle转换为Gun形态的时间
                }
                break;

            case BossState.MeleeAttack:
                currentState = BossState.Idle;
                animator.Play("Idle");
                stateTimer = 2f; // 停留在Idle状态的时间
                break;

            case BossState.GunTransition:
                currentState = (Random.Range(0, 2) == 0) ? BossState.GunAttackLeft : BossState.GunAttackRight;
                if (currentState == BossState.GunAttackLeft)
                {
                    animator.Play("GunMoveLeft");
                }
                else
                {
                    animator.Play("GunMoveRight");
                }
                stateTimer = 2f; // 移动并射击的时间
                break;

            case BossState.GunAttackLeft:
            case BossState.GunAttackRight:
                currentState = BossState.Idle;
                animator.Play("Idle");
                stateTimer = 2f; // 停留在Idle状态的时间
                break;
        }
    }

    private void PerformMeleeAttack()
    {
        // 实现近战攻击逻辑
    }

    private void PerformGunAttack()
    {
        // 实现枪攻击逻辑
    }
    public void TakeDamage(float damage)
    {
        float realDamage = damage*bossParameter.BossDamageMultiplier;
        bossParameter.BossCurrentHealth -= realDamage; // 减少血量
        Debug.Log($"Enemy took {realDamage} damage, remaining health: {bossParameter.BossCurrentHealth}");

        if (bossParameter.BossCurrentHealth <= 0)
        {
            Instantiate(NextRoomObject,gameObject.transform.position,Quaternion.identity);
            Die();
        }
    }

    public void Die()
    {
        Debug.Log("Boss died!");
        
        Destroy(gameObject); // 销毁敌人对象
    }

    public void BossShotEvent1()
    {
        audioSource.PlayOneShot(ShotSound);
        
        Transform firePoint = FirePoint[0];

        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent2()
    {
        audioSource.PlayOneShot(ShotSound);
        Transform firePoint = FirePoint[1];

        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent3()
    {
        audioSource.PlayOneShot(ShotSound);
        Transform firePoint = FirePoint[2];

        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }

    public void BossShotEvent4()
    {
        Transform firePoint = FirePoint[3];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent5()
    {
        Transform firePoint = FirePoint[4];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent6()
    {
        Transform firePoint = FirePoint[5];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent7()
    {
        Transform firePoint = FirePoint[6];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent8()
    {
        Transform firePoint = FirePoint[7];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent9()
    {
        Transform firePoint = FirePoint[8];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent10()
    {
        Transform firePoint = FirePoint[9];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent11()
    {
        Transform firePoint = FirePoint[10];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent12()
    {
        Transform firePoint = FirePoint[11];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent13()
    {
        Transform firePoint = FirePoint[12];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent14()
    {
        Transform firePoint = FirePoint[13];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent15()
    {
        Transform firePoint = FirePoint[14];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent16()
    {
        Transform firePoint = FirePoint[15];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent17()
    {
        Transform firePoint = FirePoint[16];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent18()
    {
        Transform firePoint = FirePoint[17];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent19()
    {
        Transform firePoint = FirePoint[18];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent20()
    {
        Transform firePoint = FirePoint[19];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent21()
    {
        Transform firePoint = FirePoint[20];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent22()
    {
        Transform firePoint = FirePoint[21];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent23()
    {
        Transform firePoint = FirePoint[22];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, Quaternion.identity);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }
    public void BossShotEvent24()
    {
        Transform firePoint = FirePoint[23];
        audioSource.PlayOneShot(ShotSound);
        GameObject bullet = Instantiate(BossBulletPrefab, firePoint.position, BossBulletPrefab.transform.rotation);

        // 获取子弹的刚体组件
        Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();

        // 设置子弹的速度和方向
        bulletRb.velocity = new Vector2(0, -bossParameter.BossBulletSpeed); // 根据需要调整方向
    }

}
